﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calculator
{
    public partial class Form1 : Form
    {
        //public方便单元测试
        public string currentInput = "";
        public double result = 0;
        public Stack<double> numbers = new Stack<double>();
        public Stack<string> operators = new Stack<string>();
        public Form1()
        {
            InitializeComponent();
        }
        //获取运算符优先级
        private int GetOperatorPrecedence(string op)
        {
            switch (op)
            {
                case "+":
                case "-":
                    return 1;
                case "*":
                case "/":
                    return 2;
                default:
                    return 0;
            }
        }
        //计算表达式
        public void PerformOperation()
        {
            double secondNumber = numbers.Pop();
            double firstNumber = numbers.Pop();
            string op = operators.Pop();
            double result = 0;

            switch (op)
            {
                case "+":
                    result = firstNumber + secondNumber;
                    break;
                case "-":
                    result = firstNumber - secondNumber;
                    break;
                case "*":
                    result = firstNumber * secondNumber;
                    break;
                case "/":
                    if (secondNumber != 0)
                        result = firstNumber / secondNumber;
                    else
                    {
                        MessageBox.Show("除数不能为0", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;
            }

            numbers.Push(result);
        }
        public void btnNumber_Click(object sender, EventArgs e)
        {
            currentInput += ((Button)sender).Text;
            lblOutput.Text += ((Button)sender).Text;
        }
        public void btnOperator_Click(object sender, EventArgs e)
        {
            //推入当前数字输入
            if (!string.IsNullOrEmpty(currentInput))
            {
                numbers.Push(Convert.ToDouble(currentInput));
                currentInput = "";
            }
            //获取运算符
            string Operator = ((Button)sender).Text;
            lblOutput.Text += Operator;

            while (operators.Count > 0 && GetOperatorPrecedence(operators.Peek()) >= GetOperatorPrecedence(Operator))
            {
                PerformOperation();
            }

            operators.Push(Operator);
        }
        public void btnEquals_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(currentInput))
            {
                MessageBox.Show("请输入完整的表达式", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //推入当前数字输入，(运算符将会变为空字符串)          
            numbers.Push(Convert.ToDouble(currentInput));
            currentInput = "";
            while (operators.Count > 0)
            {
                PerformOperation();
            }
            if (numbers.Count == 1)
            {
                result = numbers.Pop();
                lblOutput.Text = result.ToString();
            }
            else
            {
                MessageBox.Show("表达式不完整", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                numbers.Clear();
                operators.Clear();
                currentInput = "";
                lblOutput.Text = "";
            }
        }
        public void btnAllClear_Click(object sender, EventArgs e)
        {
            lblOutput.Text = "";
            currentInput = "";
            result = 0;
            numbers.Clear();
            operators.Clear();
        }
    }
}
